package andev.axplorer.Service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import java.util.ArrayList;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by lzy1g on 2015/8/8.
 */
public class CrawlDirService extends Service {
    public boolean crawlEnd = false;
    private ArrayList<Map<String, Object>> data;
    private String search;
    private Binder binder = new CralDirServiceBinder();

    public ArrayList<Map<String, Object>> getAdapterData() {
        return data;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Bundle bundle = intent.getExtras();
        search = bundle.getString("search");

        final Pattern pattern = Pattern.compile("\\/[^\\/]*" + search + "[^\\/]*(\\/?)([^\\/]*)");
        data = new ArrayList<>();
        /*new FileOperator().crawlDir(500, Environment.getExternalStorageDirectory(), new FileOperator.CrawlDirListener() {
            @Override
            public void onCrawl(String absolutePath) {
                Matcher matcher = pattern.matcher(absolutePath);
                if (matcher.find()) {
                    if (!"".equals(matcher.group(1)) && !"".equals(matcher.group(2)))
                        return;
                    MainActivity.makeTileAdapter(new File(absolutePath), data);
                }
            }

            @Override
            public void onCrawlEnd() {
                crawlEnd = true;
            }
        });*/
        return binder;
    }

    @Override
    public void onCreate() {
        Log.i("", "onCreate");
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public class CralDirServiceBinder extends Binder {
        public CrawlDirService getCrawlDirService() {
            return CrawlDirService.this;
        }
    }
}
